﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using WebUI.SmartDatingRef;

namespace WebUI {
    public partial class EditPhoto : BasePage {
        protected global::System.Web.UI.HtmlControls.HtmlInputButton cmdReset;
        protected global::System.Web.UI.HtmlControls.HtmlInputButton cmdUpload;

        protected void cmdReset_ServerClick(object sender, EventArgs e) {
            OperationStatus statusSetImg = Client.SetImage(SessionKeyHash, null);
            if (!Status.SUCCESS(statusSetImg)) {
                Utils.showError(errSpan, Status.m(statusSetImg));
                return;
            }
        }

        protected void cmdUpload_ServerClick(object sender, EventArgs e) {
            
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (fuPhoto.HasFile) {
                string fileName = fuPhoto.FileName.ToLower();
                if (!fileName.EndsWith(".jpeg") && !fileName.EndsWith(".jpg")) {
                    Utils.showError(errSpan, "Загружаемая фотография должна иметь расширение .jpg или .jpeg.");
                    return;
                }

                const int MAX_STREAM_LEN = 6 * 1024;
                if (fuPhoto.FileContent.Length > MAX_STREAM_LEN) {
                    Utils.showError(errSpan, "Загружаемая фотография должна иметь размер не более 6 Кб.");
                    return;
                }

                // get file bytes
                Stream photoStream = fuPhoto.PostedFile.InputStream;
                byte[] photoBytes = new byte[photoStream.Length];
                photoStream.Read(photoBytes, 0, photoBytes.Length);
                // to base 64 and save to service
                OperationStatus statusSetImg = Client.SetImage(SessionKeyHash, s(photoBytes));
                if (!Status.SUCCESS(statusSetImg)) {
                    Utils.showError(errSpan, Status.m(statusSetImg));
                }
            }

            imgPhoto.Src = "Photo.aspx?large=true&id=" + Request.Params["id"];
        }
    }
}